const { controller } = require('./pages/controllers/Controller');

const Controller = require('/pages/controllers/Controller.js').controller;
App({
  onLaunch: function(option) {
    //获取usertoken
    let usertoken = wx.getStorageSync('usertoken') || '';
    //优先判断usertoken
    if (usertoken) {
      this.GetLogin();
      //延时判断微信服务器Session状态
      setTimeout(() => {
        wx.checkSession({
          success: () => {
            //session_key 未过期，并且在本生命周期一直有效
          },
          fail: () => {
            // session_key 已经失效，需要重新执行登录流程
            // this.GetLogin(false);
          }
        });
      }, 1500);
    } else {
      //无usertoken，直接登录
      this.GetLogin();
    }

    //异步更新 + 强制更新

    // wx.getUpdateManager 在 1.9.90 才可用，请注意兼容
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function(res) {
      // 请求完新版本信息的回调
      // console.log(res.hasUpdate)
      if(res.hasUpdate){
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否马上重启小程序？',
          success: function(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        });
      }
    });

    updateManager.onUpdateReady(function() {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否马上重启小程序？',
        success: function(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      });
    })

    updateManager.onUpdateFailed(function() {
      wx.showModal({
         title: '已有新版本',
         content: '新版本已经上线了，请删除当前小程序，重新搜索打开',
      })
    });

    // 获取手机系统信息
    wx.getSystemInfo({
      success: res => {
        //导航高度
        this.globalData.navHeight = res.statusBarHeight + 46;
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  ToLogin(msg) {
    var that=this;
    if(that.globalData.islogin){
      that.globalData.islogin=false
      wx.login({
        success: res => {
          Controller.appletLogin({
            code: res.code
          }).then(result => {
            console.log(result)
            if(result.data.sessionKey){
              this.globalData.sessionKey=result.data.sessionKey
            }
            if(result.data.sessionId){
              wx.setStorageSync('usertoken', result.data.sessionId)
            }else{
              this.ToLoginlink()
            }
            this.globalData.openId=result.data.openId
            console.log(this.globalData.openId)
            if (this.usertokenReadyCallback) {
              this.usertokenReadyCallback(result);
            }
            
          }).catch(res=>{
            this.ToLoginlink()
          })
          
        }
      });
      
    }
  },
  ToLoginlink(){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否授权登录?',
      success(res) {
        that.globalData.islogin=true
        if (res.confirm) {
          wx.navigateTo({
            url: "/pages/Login/Login"
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //登录请求
  GetLogin(flag) {
    if (flag == undefined || flag) {
      wx.showLoading({
        title: '登录中...',
        mask: true
      });
    }
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        Controller.appletLogin({
          code: res.code
        }).then(result => {
          console.log(result.data)
          if(result.data.sessionKey){
            this.globalData.sessionKey=result.data.sessionKey
          }
          if(result.data.sessionId){
            wx.setStorageSync('usertoken', result.data.sessionId)
          }else{
            this.ToLoginlink()
          }
          // if(result.data.sessionId){
          //   wx.setStorageSync('usertoken', result.data.sessionId)
          // }
          this.globalData.openId=result.data.openId
          if (this.usertokenReadyCallback) {
            this.usertokenReadyCallback(result);
          }
          
          if (flag == undefined || flag) {
            wx.hideLoading();
          }
        }).catch(res=>{
          this.ToLoginlink()
          wx.hideLoading();
        })
        
      }
    });
  },


  //公共变量
  globalData: {
    islogin:true,
    IMG_URL:'http://111.230.226.223/beauty-industry',
    mobile:'',
    shopId:'',
    myshopinfo:'',
    sessionKey:'',
    openId:'',
    globalStype:'pinStyle', //1粉色 2黄色
    tabbar:[
      {
        "text": "首页",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_home.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_home_pre.png",
      },
      {
        "text": "商城",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_store.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_store_pre.png",
      },
      {
        "text": "购物车",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_buy.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_buy_pre.png",
      },
      {
        "text": "我的",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_my.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_my_pre.png",
      }
    ],
    tabbar2:[
      {
        "text": "首页",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_home.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/o2o/yellow/tab_home_pre.png",
      },
      {
        "text": "商城",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_store.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/o2o/yellow/tab_store_pre.png",
      },
      {
        "text": "购物车",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_buy.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/o2o/yellow/tab_buy_pre.png",
      },
      {
        "text": "我的",
        "iconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/tab_my.png",
        "selectedIconPath": "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/o2o/yellow/tab_my_pre.png",
      }
    ],
    contentList:[],
    imguploadurl:'https://www.xlshhr.com/beauty-industry',
    coupon:null,
    isuseCoupon:1,//1用优惠券 2不用优惠券
    vipDesc:""
  },
  //拨打电话
  calling: function(e) {
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: e || this.globalData.mobile,
      success: function() {
        console.log("拨打电话成功！")
      },
      fail: function() {
        console.log("拨打电话失败！")
      }
    })
  }

})